﻿using System;
using RabbitMQ.Client;

namespace ConsoleAppFramework.Rmq
{
    public class RmqFailService
    {
        public string QueueName { get; set; }

        public RmqUrlEnum RmqUrl { get; set; }

        protected string FailQueueName => RmqBasicInfoConfig.BaseFailQueuePrefix + QueueName;

        public RmqFailService(string queueName, RmqUrlEnum rmqUrl)
        {
            QueueName = queueName;
            RmqUrl = rmqUrl;
            try
            {
                using IModel model = RmqConnectPoolFactory.GetClientPool(RmqUrl).GetConnect().GetChannel();
                model.QueueDeclare(FailQueueName, durable: true, exclusive: false, autoDelete: false, null);
                model.QueueBind(FailQueueName, RmqBasicInfoConfig.BaseExchange, FailQueueName, null);
            }
            catch (Exception ex)
            {
                RmqLogHelper.WriteError("FailQueueInit 初始化错误队列异常：" + ex.ToString());
            }
        }

        public void FailSend<T>(T t)
        {
            try
            {
                RmqMessageProducer.MessageSend(RmqUrl, RmqBasicInfoConfig.BaseExchange, FailQueueName, t);
            }
            catch (Exception ex)
            {
                RmqLogHelper.WriteError("FailSend处理错误消息异常：" + ex.ToString());
            }
        }
    }
}
